package zfj.com.myretrofitdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.GsonConverterFactory;
import retrofit2.Response;
import retrofit2.Retrofit;

public class MainActivity extends AppCompatActivity {
    public static final String API_URL = "https://api.github.com";
    Button button1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        button1 = (Button) findViewById(R.id.button1);
        setContentView(R.layout.activity_main);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.github.com")
                //添加一个转换器
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        //创建API接口对象
        APIInterface service = retrofit.create(APIInterface.class);
        //使用APIInterface创建一个请求对象,.repo("Guolie1130")取代了前面的{user}
        // 访问地址替换为：https://api.github.com/users/Guolei1130
        Call<TestModel> model = service.repo("Guolei1130");
        model.enqueue(new Callback<TestModel>() {
            @Override
            public void onResponse(Response<TestModel> response) {
                Log.e("MianActivity", response.body().getLogin());
            }

            @Override
            public void onFailure(Throwable t) {
                Log.e("MainActivity", t.getMessage());
            }
        });

    }

    public void onClick(View view) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.github.com")
                //添加一个转换器
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        //创建API接口对象
        APIInterface service = retrofit.create(APIInterface.class);
        //使用APIInterface创建一个请求对象,.repo("Guolie1130")取代了前面的{user}
        // 访问地址替换为：https://api.github.com/users/Guolei1130
        Call<TestModel> model = service.repo("Guolei1130");
        model.enqueue(new Callback<TestModel>() {
            @Override
            public void onResponse(Response<TestModel> response) {
                Log.e("MianActivity", response.body().getLogin().toString());
            }

            @Override
            public void onFailure(Throwable t) {
                Log.e("MainActivity", t.getMessage());
            }
        });

    }
}
